// pages/promotion/activity.js
const App = getApp();

// 富文本插件
import wxParse from '../../wxParse/wxParse.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareIcon: '', //分享的封面图
    // 文章详情
    info: {},
    promotion:{},
    showButton: false,
    referrerInfo:'',
    statistics_id:0,
    _openid:'',
    pid:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.referrerInfo && options.referrerInfo.appId) {
      const { appId, extraData } = options.referrerInfo;
      this.setData({
        referrerInfo: extraData
      });
    }

    this.setData({
      pid:options.pid
    });

   // 设置navbar标题、颜色
   App.setNavigationBar();
  },

  
  onReady(){
    this.getArticleDetail(this.data.pid);
  },
  /**
   * 获取文章详情
   */
  getArticleDetail(pid) {
    let _this = this;
    App._post_form('promotion/activity', {
      openid: wx.getStorageSync('user_openid'),
      pid:pid
    }, function (result) {
      console.log(result.data);
      let info = result.data.info;
      let promotion = result.data.promotion;

      wx.setNavigationBarTitle({
        title: info.title
      });

      _this.setData({
        kefu:result.data.kefu,
        info: info,
        promotion: promotion,
        pid:promotion.id,
        shareIcon:info.thumb
      });
      _this.statistics();
      console.log('获取到了文章详情：', info);
      // 富文本转码
      if (info.details.length > 0) {
        wxParse.wxParse('content', 'html', info.details, _this, 0);
      }
    if(info.trigger_method=='time'){
      setTimeout(function() { _this.openCustomerService(promotion.question); }, info.communication_duration*1000);
    }else if(info.trigger_method=='click'){
      _this.setData({ showButton: true });
    }else{
      _this.setData({ showButton: true });
    }
  
    });
  
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    // 构建页面参数
    const params = App.getShareUrlParams({
      'pid': this.data.pid
    });
    return {
      imageUrl: this.data.shareIcon,
      title: this.data.info.title,
      path: "/pages/promotion/activity?" + params
    };
  },
  openCustomerService(msg) {
    let _this = this;
    wx.showModal({
      title: '确认对话框',
      content: msg || '您好，我是您的专属客服代表，我将竭诚为您提供帮助和解答。 ',
      success: (res) => {
        if (res.confirm) {

          wx.openCustomerServiceChat({
            extInfo: {url: _this.data.kefu.url},
            corpId: _this.data.kefu.corpId,
            success(res) { console.log('成功打开客服会话'); },
            error(){
              _this.setData({ showButton: true });
            }
          })
        } else if (res.cancel) {
          // 用户点击了取消按钮
          console.log('用户取消了操作');
        }
      }
    });



  },

  /**
   * 分享到朋友圈
   * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
   * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
   */
  onShareTimeline() {
    // 构建页面参数
    const params = App.getShareUrlParams({
      'pid': this.data.promotion.id
    });
    return {
      // title: this.data.detail.article_title,
      path: "/pages/promotion/activity?" + params
    };
  },
  /* 统计 */
  statistics(){
    var screen = '未知';
    wx.getSystemInfo({
      success(res) {
        const { screenWidth, screenHeight } = res;
        screen = screenWidth +'x'+ screenHeight;
      }
    });
    var _this = this;
    App._post_form('promotion/statistics', {
      'openid': wx.getStorageSync('user_openid'),
      'currentUrl':'小程序内部地址',
      'referrer_url':_this.data.referrerInfo,
      'pid':_this.data.promotion.id,
      'id':_this.data.statistics_id,
      'screen':screen
    }, function (result) {
      _this.data.statistics_id = result.data.id;
    });

  }

})